package utils;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import org.json.JSONObject;

import java.util.Random;

import app.Main;
import application.AppApplication;
import bean.WorkFlow;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by LaoZhao on 2017/11/19.
 */

public class NotificationUtils extends ContextWrapper {

    private NotificationManager manager;
    public static final String id = "channel_1";
    public static final String name = "channel_name_1";
    private int notifyNum = new Random().nextInt(1000);
    public NotificationUtils(Context context) {
        super(context);
    }

    @TargetApi(Build.VERSION_CODES.O)
    public void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }

    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }

    @TargetApi(Build.VERSION_CODES.O)
    public Notification.Builder getChannelNotification(String title, String content) {
        Notification.Builder builder = new Notification.Builder(getApplicationContext(), id)
                .setContentTitle(title)
                .setOnlyAlertOnce(true)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setSmallIcon(android.R.drawable.stat_notify_more)
                .setAutoCancel(true);
        try {
            JSONObject jsonObject = new JSONObject(content);
            builder.setContentText(jsonObject.optString("title"));
            String type = jsonObject.optString("type");
            if (type.equals("Workflow")) {
                String mid = jsonObject.optJSONObject("content").optString("mid");
                WorkFlow workFlow = new WorkFlow();
                workFlow.setMessageId(mid);
                PendingIntent intent = PendingIntent.getActivity(getBaseContext(), notifyNum, new Intent("com.i8office.android.zhihuixinwu.ui.DetailActivity")
                        .putExtra("workFlow", workFlow)
                        .putExtra("from", "main")
                        .putExtra("isNotify", true).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(intent);
            }

        } catch (Exception e) {

        }
        return builder;
    }

    public NotificationCompat.Builder getNotification_25(String title, String content) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
                .setContentTitle(title)
                .setOnlyAlertOnce(true)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setSmallIcon(android.R.drawable.stat_notify_more)
                .setAutoCancel(true);
        try {
            JSONObject jsonObject = new JSONObject(content);
            builder.setContentText(jsonObject.optString("title"));
            String type = jsonObject.optString("type");
            if (type.equals("Workflow")) {
                String mid = jsonObject.optJSONObject("content").optString("mid");
                WorkFlow workFlow = new WorkFlow();
                workFlow.setMessageId(mid);
                PendingIntent intent = PendingIntent.getActivity(getBaseContext(), notifyNum, new Intent("com.i8office.android.zhihuixinwu.ui.DetailActivity")
                        .putExtra("workFlow", workFlow)
                        .putExtra("from", "main")
                        .putExtra("isNotify", true).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(intent);
                builder.setDeleteIntent(intent);
            }

        } catch (Exception e) {

        }
        return builder;
    }

    public void sendNotification(String title, String content) {
        if (Build.VERSION.SDK_INT >= 26) {
            createNotificationChannel();
            Notification notification = getChannelNotification
                    (title, content)
                    .build();
            getManager().notify(notifyNum, notification);
        } else {
            Notification notification = getNotification_25(title, content).build();
            getManager().notify(notifyNum, notification);
        }
    }
}